import {ACTION_TYPE, DISPLAY_TYPE,} from "../../../../common/actions/FlowActions";
import {isEmptyObj} from "../../../../utils/collectionUtils";
import {
  getModalContent,
  getStepsToCreateFlow,
  getStepsToCreateProcess,
  getStepsToCreateProcessProvider,
  getStepsToCreateResource
} from "./modalContentUtils";
import React from "react";
import StepWrapper from "../form/StepWrapper";


export function getModalBodyHeight(displayType, recordCount) {
  switch (displayType) {
    case DISPLAY_TYPE.JSON:
      return getHeightForPropertyModalBody(recordCount);
      break;
    case DISPLAY_TYPE.PROPERTY:
      return getHeightForJsonModalBody();
      break;
    default:
      return MAXIMUM_MODAL_BODY_HEIGHT;
  }
}


const MINIMUM_MODAL_BODY_HEIGHT = 350

const MAXIMUM_MODAL_BODY_HEIGHT = 500

const ROW_HEIGHT = 27

const OTHER_HEIGHT_FOR_MODAL_BODY = 24 * 2 + 31 + 31 //padding * 2 + column hider + with of scroll bar

const getHeightForPropertyModalBody = (recordCount) => {
  if (isEmptyObj(recordCount)) {
    return MINIMUM_MODAL_BODY_HEIGHT
  }

  if (recordCount * ROW_HEIGHT > MAXIMUM_MODAL_BODY_HEIGHT) {
    return MAXIMUM_MODAL_BODY_HEIGHT
  }

  return recordCount * ROW_HEIGHT + OTHER_HEIGHT_FOR_MODAL_BODY
}

const getHeightForJsonModalBody = () => {
  return MAXIMUM_MODAL_BODY_HEIGHT
}


const MINIMUM_MODAL_BODY_WIDTH = 500

const MAXIMUM_MODAL_BODY_WIDTH = 1000

export function getModalBodyWidth(displayType) {
  switch (displayType) {
    case DISPLAY_TYPE.JSON:
      return MAXIMUM_MODAL_BODY_WIDTH;
      break;
    case DISPLAY_TYPE.PROPERTY:
      return MINIMUM_MODAL_BODY_WIDTH;
      break;
    default:
      return MAXIMUM_MODAL_BODY_HEIGHT;
  }
}

export function getDisplayType(cellActionType) {
  switch (cellActionType) {
    case ACTION_TYPE.VIEW_JSON:
    case ACTION_TYPE.UPDATE_JSON:
      return DISPLAY_TYPE.JSON;
      break;
    case ACTION_TYPE.VIEW_PROPERTY:
    case ACTION_TYPE.UPDATE_PROPERTY:
      return DISPLAY_TYPE.PROPERTY;
      break;
    default:
      return null;
  }
}

export function setModalInStateForViewUpdate(modalOptions, action, record) {
  if(!modalOptions){
    throw new Error("The modalOptions should not be undefined")
  }

  if (!action) {
    throw new Error("The action should not be undefined")
  }

  if (!record) {
    throw new Error("The record should not be undefined")
  }

  modalOptions.modalProperties = getModalProperties(action, record)
  modalOptions.modalProperties["visible"] = true
  modalOptions.modalContent = getModalContent(action, record)
}

export function setModalInStateForCreate(modalOptions, action) {
  if(!modalOptions){
    throw new Error("The modalOptions should not be undefined")
  }

  if (!action) {
    throw new Error("The action should not be undefined")
  }

  let steps = []
  let modalTitle = "";
  switch (action) {
    case ACTION_TYPE.CREATE_FLOW:
      steps = getStepsToCreateFlow();
      modalTitle="Create Flow";
      break;
    case ACTION_TYPE.CREATE_PROCESS:
      steps = getStepsToCreateProcess();
      modalTitle="Create Process";
      break;
    case ACTION_TYPE.CREATE_PROCESS_PROVIDER:
      steps = getStepsToCreateProcessProvider();
      modalTitle="Create Process Provider";
      break;
    case ACTION_TYPE.CREATE_RESOURCE:
      steps = getStepsToCreateResource();
      modalTitle="Create Resource";
      break;
  }

  modalOptions.modalProperties = {
    bodyStyle: {height: 500},
    width: 1300
  }
  modalOptions.modalProperties["visible"] = true
  modalOptions.modalProperties["title"] = modalTitle
  modalOptions.modalContent = <StepWrapper steps={steps}/>
}


export function popupModalToCreate(modalOptions, modalTitle, steps) {
  if(!modalOptions){
    throw new Error("The modalOptions should not be undefined")
  }

  modalOptions.modalProperties = {
    bodyStyle: {height: 500},
    width: 1300
  }
  modalOptions.modalProperties["visible"] = true
  modalOptions.modalProperties["title"] = modalTitle
  modalOptions.modalContent = <StepWrapper steps={steps}/>
}

export function getModalProperties(cellActionType, record) {
  const displayType = getDisplayType(cellActionType)
  const modalBodyHeight = getModalBodyHeight(displayType, record)
  const modalBodyWidth = getModalBodyWidth(displayType)
  const bodyStyle = {height: modalBodyHeight}

  return {
    bodyStyle: bodyStyle,
    width: modalBodyWidth
  }
}
